package sdk.org.apache.shiro.G2_使用.C10_SpringBoot集成_1登录和退出.shiro;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import com.titan.core.utils.file.JsonUtils;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.domain.SysUserDomain;
import sdk.org.apache.shiro.utils.ShiroUtils;

public class MyShiroRealm extends AuthorizingRealm {
    private final static Map<String,SysUserDomain> users = new HashMap<String,SysUserDomain>();
    
    public static void putAccount(long id, String loginid, String password, String username) {
        SysUserDomain domain = new SysUserDomain();
        domain.setId(id);
        domain.setLoginid(loginid);
        domain.setPassword(ShiroUtils.getPassword(loginid, password));
        domain.setUsername(username);
        users.put(loginid, domain);
    }
    
    
    //角色权限和对应权限添加
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String loginid = ShiroUtils.getUsername(principalCollection, this);
//        ConsoleUtils.method(MyShiroRealm.class, "doGetAuthorizationInfo", "loginid", loginid);
        //添加角色和权限
        Set<String> roles = new HashSet<String>();
        Set<String> permissions = new HashSet<String>();
        
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(roles);
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }
    
    
    //用户认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 获取用户信息
        String loginid = ShiroUtils.getUsername(token);
        String password = ShiroUtils.getPassword(token);
        SysUserDomain user = users.get(loginid);
        // 账号不存在
        if(user == null) {
            throw new UnknownAccountException("账号不存在！");
        }
        try {
            ConsoleUtils.method(MyShiroRealm.class, "doGetAuthenticationInfo", "loginid", loginid, "json", JsonUtils.toString(user));
        } catch (Exception e) {
            e.printStackTrace();
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(loginid), getName());
        return info;
    }
}
